package kr.co.coffee247soft.cafe;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import kr.co.coffee247soft.request.ParserHandler;

public class CafeParser extends ParserHandler
{
	private StringBuilder builder;
	private List<CafeData> list = new ArrayList<CafeData>();
	private CafeData data;

	@Override
	public List<?> getList()
	{
		return list;
	}
	
	@Override
	public void startDocument() throws SAXException 
	{
		super.startDocument();
		
		builder = new StringBuilder();
	}
	
	@Override
	public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException 
	{
		super.startElement(uri, localName, name, attributes);
		
		if (localName.equalsIgnoreCase("contents"))
		{
			data = new CafeData();
		}
	}
	
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException 
	{
		super.characters(ch, start, length);
		
		builder.append(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String name) throws SAXException {
		super.endElement(uri, localName, name);
		
		if( this.data != null )
		{			
			if(localName.equalsIgnoreCase("id"))
			{
				data.setId(builder.toString().trim());
			}
			if(localName.equalsIgnoreCase("c_name"))
			{
				data.setC_name(builder.toString().trim());
			}
			if(localName.equalsIgnoreCase("lon"))
			{
				data.setLon(builder.toString().trim());
			}
			if(localName.equalsIgnoreCase("lat"))
			{
				data.setLat(builder.toString().trim());
			}
			if(localName.equalsIgnoreCase("brand_id"))
			{
				data.setBrand_id(builder.toString().trim());
			}
			if(localName.equalsIgnoreCase("brand_name"))
			{
				data.setBrand_name(builder.toString().trim());
			}
			if(localName.equalsIgnoreCase("rate"))
			{
				data.setRate(builder.toString().trim());
			}
			else if( localName.equalsIgnoreCase("contents"))
			{
				list.add(data);
			}
		}
		
		builder.setLength(0);	
	}
}
